package com.etop.learnapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etop.learnapp.MyItemClickListener;
import com.etop.learnapp.R;

/**
 * Created by John on 2015/10/26 0026.
 */
public class ChapterAdapter extends RecyclerView.Adapter<ChapterAdapter.ViewHolder>  {
    private final int[] resIDs;
    private String[] mDataset;
    private MyItemClickListener mItemClickListener;

    public  class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public TextView mTextView;
        public ImageView mImageView;
        private MyItemClickListener mListener;
        public ViewHolder(View itemView,MyItemClickListener mItemClickListener)
        {
            super(itemView);
            mListener = mItemClickListener;
            mTextView = (TextView) itemView.findViewById(R.id.tv_item);
            mImageView = (ImageView) itemView.findViewById(R.id.iv_item);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(mListener!=null)
            {
                mListener.onItemClick(view,getLayoutPosition());
            }
        }
    }
    public ChapterAdapter(String[] mDataset,int[] resIDs)
    {
        super();
        this.mDataset = mDataset;
        this.resIDs = resIDs;
    }
    @Override
    public ChapterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_view, parent, false);
        ViewHolder vh = new ViewHolder(v,mItemClickListener);
        return vh;
    }
    public void setOnItemClickListener(MyItemClickListener listener){
        this.mItemClickListener = listener;
    }

    @Override
    public void onBindViewHolder(ChapterAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
        holder.mImageView.setImageResource(resIDs[position%27]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }

}
